package org.rency.mq.activemq.request;

import org.rency.mq.activemq.constant.AcknowledgeMode;
import org.rency.mq.activemq.constant.DeliveryMode;
import org.rency.mq.activemq.constant.DestinationType;
import org.rency.mq.activemq.constant.MessageFormat;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 基本消息请求
 *
 * 主要包括创建Destination的基本信息，以及重试队列和失败队列等，应用中使用的请求应该继承本类。
 *
 * @author: Created by user_rcy@163.com on 2016/10/19.
 * @version: V1.0.0
 * @since JDK 1.8
 */
public abstract class AbstractRequest implements MQRequest {

    /**
     * ActionId
     */
    private int actionId;
    /**
     * 目的地名称
     */
    private String destination;
    /**
     * 目的地类型，默认为QUEUE
     */
    private int destinationType = DestinationType.QUEUE;
    /**
     * 传输模式，默认为非持久化
     */
    private int deliveryMode = DeliveryMode.NON_PERSISTENT;
    /**
     * 应答模式，默认为自动应答
     */
    private int acknowledgeMode = AcknowledgeMode.AUTO_ACKNOWLEDGE;
    /**
     * 是否需要事务，默认为否
     */
    private boolean transacted = false;
    /**
     * 请求时间
     */
    private Date requestTime = new Date();
    private int requestType;
    private long expirationTime;
    private int messageFormat = MessageFormat.OBJECT;
    private Map<String, Object> properties;

    public int getActionId() {
        return actionId;
    }

    public void setActionId(int actionId) {
        this.actionId = actionId;
    }

    public String getDestination() {
        return destination;
    }

    public void setDestination(String destination) {
        this.destination = destination;
    }

    public int getDestinationType() {
        return destinationType;
    }

    public void setDestinationType(int destinationType) {
        this.destinationType = destinationType;
    }

    public int getDeliveryMode() {
        return deliveryMode;
    }

    public void setDeliveryMode(int deliveryMode) {
        this.deliveryMode = deliveryMode;
    }

    public Date getRequestTime() {
        return requestTime;
    }

    public void setRequestTime(Date requestTime) {
        this.requestTime = requestTime;
    }

    public int getAcknowledgeMode() {
        return acknowledgeMode;
    }

    public void setAcknowledgeMode(int acknowledgeMode) {
        this.acknowledgeMode = acknowledgeMode;
    }

    public boolean isTransacted() {
        return transacted;
    }

    public void setTransacted(boolean transacted) {
        this.transacted = transacted;
    }

    public int getRequestType() {
        return requestType;
    }

    public void setRequestType(int requestType) {
        this.requestType = requestType;
    }

    public long getExpirationTime() {
        return expirationTime;
    }

    public void setExpirationTime(long expirationTime) {
        this.expirationTime = expirationTime;
    }

    public int getMessageFormat() {
        return messageFormat;
    }

    public void setMessageFormat(int format) {
        this.messageFormat = format;
    }

    public Map<String, Object> getProperties() {
        return properties;
    }

    public void setProperties(Map<String, Object> properties) {
        this.properties = properties;
    }

    public void addProperty(String key, Object value) {
        if (this.properties == null) {
            this.properties = new HashMap<String, Object>();
        }

        this.properties.put(key, value);
    }

    public Object getProperty(String key){
        if(this.properties == null)return null;

        return this.properties.get(key);
    }

    public boolean containsProperty(String key){
        if (this.properties == null) return false;

        return this.properties.containsKey(key);
    }

    protected void copyProperties(MQRequest dest){
        if(dest == null)return;

        dest.setAcknowledgeMode(acknowledgeMode);
        dest.setActionId(actionId);
        dest.setDeliveryMode(deliveryMode);
        dest.setDestination(destination + "");
        dest.setDestinationType(destinationType);
        dest.setExpirationTime(expirationTime);
        dest.setMessageFormat(messageFormat);
        dest.setProperties(properties);
        dest.setRequestTime(requestTime);
        dest.setRequestType(requestType);
        dest.setTransacted(transacted);
    }

    @Override
    public String toString() {
        return "AbstractRequest [actionId=" + actionId + ", destination="
                + destination + ", destinationType=" + destinationType
                + ", deliveryMode=" + deliveryMode + ", acknowledgeMode="
                + acknowledgeMode + ", transacted=" + transacted
                + ", requestTime=" + requestTime + ", requestType="
                + requestType + ", expirationTime=" + expirationTime
                + ", messageFormat=" + messageFormat + ", properties="
                + properties + "]";
    }

}